//
//  XMLoginOtherPhoneController.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/8.
//

#import "XMLoginOtherPhoneController.h"
#import "XMLoginOtherPhoneView.h"
#import "XMPhoneCodeViewController.h"
#import "XMNetwork+login.h"
#import <WXApi.h>

#import "XMBindPhoneViewController.h"
#import "XMServiceInfoViewController.h"

@interface XMLoginOtherPhoneController ()<XMLoginOtherPhoneViewDelegate>

@end

@implementation XMLoginOtherPhoneController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self hideNavigationBar:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    XMLoginOtherPhoneView *otherPhoneView = [[XMLoginOtherPhoneView alloc] initWithFrame:self.view.bounds];
    otherPhoneView.delegate = self;
    [self.view addSubview:otherPhoneView];
//    XMWeakSelf;
//    otherPhoneView.getCodeBlock = ^(NSString * _Nonnull phoneNumStr) {
//        [weakSelf getCodeWithPhone:phoneNumStr];
//    };
}

//获取验证码
- (void)getCodeWithPhone:(NSString *)phoneNumStr {
    [_Network getCodeWithPhoneNum:phoneNumStr
                             type:@"1"
                          success:^(id  _Nonnull responseObject) {
        XMPhoneCodeViewController *codeVC = [[XMPhoneCodeViewController alloc] init];
        codeVC.phoneNumStr = phoneNumStr;
        codeVC.codeType = @"0";
        [self.navigationController pushViewController:codeVC animated:YES];
    } failure:^(NSError * _Nonnull error) {

    }];
}

- (void)onResp:(BaseResp *)resp {
    if (!resp.errStr || resp.errStr.length == 0) {
//        UIAlertController *alertVC = [UIAlertController initwithTitle:((SendAuthResp *)resp).code message:@"" titles:@[@"确定"] cancelTitle:@"取消" itemSelect:^(NSInteger selectIndex) {
//
//        }];
//        [self presentViewController:alertVC animated:YES completion:nil];
        [self getWeichatInfoWithRespcode:((SendAuthResp *)resp).code];
    }
}

//获取微信登录信息
- (void)getWeichatInfoWithRespcode:(NSString *)respcode {
    MJWeakSelf;
    [_Network getWechatInfoWithRespcode:respcode
                                success:^(id  _Nonnull responseObject) {
        NSString *unionidStr = [responseObject safeStringForKey:@"unionid"];
        if (unionidStr && unionidStr.length > 0) {
//            [weakSelf loginWithWeichatUnionid:unionidStr];
            [weakSelf wechatHasBindPhoneWithAccessToken:[responseObject safeStringForKey:@"access_token"]
                                                 openId:[responseObject safeStringForKey:@"openid"]
                                                unionId:[responseObject safeStringForKey:@"unionid"]];
        }
        else {
            [MBProgressHUD showError:@"登录失败"];
        }
    } failure:^(NSError * _Nonnull error) {
        [MBProgressHUD showError:@"登录失败"];
    }];
}

//判断微信是否绑定手机
- (void)wechatHasBindPhoneWithAccessToken:(NSString *)accessToken
                                   openId:(NSString *)openId
                                  unionId:(NSString *)unionId {
    XMWeakSelf;
    [_Network wechatHasBindPhoneWithAccessToken:accessToken
                                         openId:openId
                                        unionId:unionId
                                        success:^(id  _Nonnull responseObject) {
        if ([responseObject safeBoolForKey:@"data"]) {
            [weakSelf wechatLoginWithAccessToken:accessToken openId:openId unionId:unionId];
        }
        else {
            XMBindPhoneViewController *bindPhoneVC = [[XMBindPhoneViewController alloc] init];
            bindPhoneVC.accessToken = accessToken;
            bindPhoneVC.openId = openId;
            bindPhoneVC.unionidStr = unionId;
            [weakSelf.navigationController pushViewController:bindPhoneVC animated:YES];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)wechatLoginWithAccessToken:(NSString *)accessToken
                            openId:(NSString *)openId
                           unionId:(NSString *)unionId {
    XMWeakSelf;
    [_Network wechatLoginWithAccessToken:accessToken
                                  openId:openId
                                 unionId:unionId
                                   phone:@"" success:^(id  _Nonnull responseObject) {
        XMUserModel *userModel = [XMUserModel yy_modelWithJSON:responseObject[@"data"]];
        [[XMUserManager shareInstance] saveUserModel:userModel];
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//微信登录
- (void)loginWithWeichatUnionid:(NSString *)unionid {
    MJWeakSelf
    [_Network wechatLoginWithCode:unionid
                          success:^(id  _Nonnull responseObject) {
        [MBProgressHUD showSuccess:@"登录成功"];
        XMUserModel *userModel = [XMUserModel yy_modelWithJSON:responseObject[@"data"]];
        [[XMUserManager shareInstance] saveUserModel:userModel];
        XMBindPhoneViewController *bindPhoneVC = [[XMBindPhoneViewController alloc] init];
        [weakSelf.navigationController pushViewController:bindPhoneVC animated:YES];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Delegate
- (void)getCodeWithPhoneNum:(NSString *)phoneNumStr {
    [self getCodeWithPhone:phoneNumStr];
}

//随便看看
- (void)lookAround {
    [self dismissViewControllerAnimated:YES completion:nil];
    UIViewController * rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    ((UITabBarController *)rootViewController).selectedIndex = 0;
}

//服务条款
- (void)checkService {
    XMServiceInfoViewController *serviceInfoVC = [[XMServiceInfoViewController alloc] init];
    serviceInfoVC.type = @"4";
    serviceInfoVC.titleStr = @"服务条款";
    [self.navigationController pushViewController:serviceInfoVC animated:YES];
}

//隐私政策
- (void)checkPrivacy {
    XMServiceInfoViewController *serviceInfoVC = [[XMServiceInfoViewController alloc] init];
    serviceInfoVC.type = @"5";
    serviceInfoVC.titleStr = @"隐私政策";
    [self.navigationController pushViewController:serviceInfoVC animated:YES];
}

//QQ登录
- (void)loginWithQQ {
    
}

//微信登陆
- (void)loginWithWechat {
//    [self getWeichatInfoWithRespcode:@"051Hp1nl2GWXRf475Tol2lnciW3Hp1nB"];
    [WXApi registerApp:@"wxe27fd4207b87851f" universalLink:@"https://help.wechat.com/zmy/"];
    SendAuthReq *req = [[SendAuthReq alloc] init];
    req.scope = @"snsapi_userinfo";
    req.state = @"com.xmjj_zmy";
    [WXApi sendReq:req completion:^(BOOL success) {

    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
